﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DuAnNH.Fd_class;


namespace DuAnNH
{
    public partial class Frm_MA : Form
    {
        public Frm_MA()
        {
            InitializeComponent();
        }
        string mb = "";
        private void Frm_MA_Load(object sender, EventArgs e)
        {
            Load_CbbLoaiTD();
            Load_DGV();
            Load_LVBan();
        }
        public void Load_CbbLoaiTD()
        {
            DataTable DTCB = new DataTable();
            DTCB = Cls_ThucDon.LayDSMaLoaiThucDon();
            Cbb_LoaiTD.DataSource = DTCB;
            Cbb_LoaiTD.DisplayMember = "TenLoai";
            Cbb_LoaiTD.ValueMember = "MaLoai";
        }
        public void Load_DGV()
        {
            DataTable DT = new DataTable();
            DT = Cls_ThucDon.LayDSThucDon(Cbb_LoaiTD.SelectedValue.ToString());
            DGV_DSTD.DataSource = DT;
        }

        private void Cbb_LoaiTD_SelectedIndexChanged(object sender, EventArgs e)
        {
            Load_DGV();
        }
        public void Load_LVBan()
        {
            DataTable tb = new DataTable();
            tb = Cls_Ban.ChonBan(0);
            load_ban(tb);
        }
        public void load_ban(DataTable tb)
        {
            LV_Ban.Items.Clear();
            for (int i = 0; i < tb.Rows.Count; i++)
            {
                ListViewItem item = new ListViewItem();
                item.SubItems[0].Text = tb.Rows[i][0].ToString();
                if (tb.Rows[i][2].ToString() == "0")
                    item.ImageIndex = 0;
                else
                {
                    if (tb.Rows[i][2].ToString() == "1")
                        item.ImageIndex = 1;
                }
                LV_Ban.Items.Add(item);
            }
        }

        private void Btn_Add_Click(object sender, EventArgs e)
        {
            if (DGV_DSTD.Enabled == true)
            {
                Btn_Save.Enabled = true;
                int idx = DGV_DSTD.CurrentRow.Index;
                string MaTD = DGV_DSTD.Rows[idx].Cells[0].Value.ToString();
                int Gia = Convert.ToInt32(DGV_DSTD.Rows[idx].Cells[2].Value);
                if (LV_DSGM.Items.Count == 0)
                {
                    int tb = Convert.ToInt32(Num_SL.Text);
                    int sl = Convert.ToInt32(DGV_DSTD.Rows[idx].Cells[2].Value);
                    ListViewItem item = new ListViewItem();
                    item.SubItems[0].Text = DGV_DSTD.Rows[idx].Cells[0].Value.ToString();
                    item.SubItems.Add(DGV_DSTD.Rows[idx].Cells[1].Value.ToString());
                    item.SubItems.Add(DGV_DSTD.Rows[idx].Cells[2].Value.ToString());
                    item.SubItems.Add(Num_SL.Text);
                    item.SubItems.Add((tb * sl).ToString());
                    LV_DSGM.Items.Add(item);
                }
                else
                {
                    bool isHasItem = false;
                    for (int i = 0; i < LV_DSGM.Items.Count; i++)
                        if (MaTD == LV_DSGM.Items[i].SubItems[0].Text)
                        {
                            int n = 0;
                            if (int.TryParse(this.Txt_SL.Text, out n))
                            {
                                int tb = Convert.ToInt32(Num_SL.Text);
                                int sl = Convert.ToInt32(LV_DSGM.Items[i].SubItems[3].Text);
                                int tt = tb + sl;
                                LV_DSGM.Items[i].SubItems[3].Text = tt.ToString();
                                LV_DSGM.Items[i].SubItems[4].Text = (Gia * tt).ToString();
                            }
                            else
                                MessageBox.Show("khong phai so nguyen");
                            isHasItem = true;
                        }
                    if (!isHasItem)
                    {
                        int tb = Convert.ToInt32(Num_SL.Text);
                        int sl = Convert.ToInt32(DGV_DSTD.Rows[idx].Cells[2].Value);
                        ListViewItem item = new ListViewItem();
                        item.SubItems[0].Text = DGV_DSTD.Rows[idx].Cells[0].Value.ToString();
                        item.SubItems.Add(DGV_DSTD.Rows[idx].Cells[1].Value.ToString());
                        item.SubItems.Add(DGV_DSTD.Rows[idx].Cells[2].Value.ToString());
                        item.SubItems.Add(Num_SL.Text);
                        item.SubItems.Add((tb * sl).ToString());
                        LV_DSGM.Items.Add(item);
                    }
                }
            }
        }

        private void Btn_Del_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in LV_DSGM.Items)
            {
                if (item.Selected)
                {
                    item.Remove();
                    if (LV_DSGM.Items.Count == 0)
                    {
                        Btn_Save.Enabled = false;
                    }
                    //LV_DSGM.Items.Remove(item);
                }
            }
        }

        private void Btn_Save_Click(object sender, EventArgs e)
        {
            int tt = 0;
            for (int i = 0; i < LV_DSGM.Items.Count; i++)
            {
                tt += int.Parse(LV_DSGM.Items[i].SubItems[4].Text);
            }
            //string mb = "";
            //mb = LV_Ban.SelectedItems.ToString();
            bool a = Cls_ThucDon.LuuHD(mb, tt.ToString());
            if (a)
            {
                for (int i = 0; i < LV_DSGM.Items.Count; i++)
                {
                    string MaTD = LV_DSGM.Items[i].SubItems[0].Text;
                    string TenTD = LV_DSGM.Items[i].SubItems[1].Text;
                    string Gia = LV_DSGM.Items[i].SubItems[2].Text;
                    string SL = LV_DSGM.Items[i].SubItems[3].Text;
                    string Tien = LV_DSGM.Items[i].SubItems[4].Text;
                    if (Cls_ThucDon.LuuCTHD(mb, MaTD, TenTD, Gia, SL, Tien))
                    {
                    }

                }
                string str = string.Format("UPDATE Ban SET TinhTrang=1 WHERE MaBan='{0}'", mb);
                SqlCommand com = new SqlCommand(str, Coso.con);
                com.ExecuteNonQuery();
                Load_LVBan();
                LV_DSGM.Items.Clear();
                //Coso.EditBan(Cbb_Ban.SelectedValue.ToString(), Cbb_Ban.SelectedValue.ToString());
            }
            else
                MessageBox.Show("abc");
        }

        private void LV_Ban_MouseClick(object sender, MouseEventArgs e)
        {
            DGV_DSTD.Enabled = true;
            mb = LV_Ban.FocusedItem.SubItems[0].Text;
        }
        
    }
}
